<template>

    <div class="store">

        <!-- 贴顶导航 -->
        <UserFixedHead title="Store Settings" />

        <!-- 初始化 -->
        <UxIniting v-if="!inited" />

        <!-- 没有店铺 -->
        <StoreEmpty title="Store Settings" v-else-if="stores.length == 0" />

        <template v-else>

            <h2>Store settings</h2>

            <h3>Choose store</h3>
            <StoreList v-model="select" :stores="stores" />

            <!-- 绑定吊牌和包装 -->
            <h3>Branding</h3>
            <StoreSettingBrand :store="selectStore" @update="getStore()" />

            <h3>Order import settings</h3>
            <div class="store-setting">
                <!-- <p v-if="selectStore.order_import_set">
                    <svg-icon name="checkbox-active" />
                    <strong>Only display orders to be fulfilled by PODpartner</strong>
                    <span>Only orders containing PODpartner products will appear on the “Orders" page.</span>
                </p> -->
                <el-radio-group v-model="selectStore.is_ignor">
                    <el-radio :label="true">
                        <strong>Automatically process orders containing non-PODpartner products.</strong>
                        <span>When an order comes in, we will automatically ignore non-PODpartner products and send the
                            rest of the items to production.</span>
                    </el-radio>
                    <el-radio :label="false">
                        <strong>Manually process orders containing non-PODpartner products.</strong>
                        <span>Imported orders containing non-PODpartner products will remain on hold. You can manually
                            ignore non-PODpartner products and send the rest to production.</span>
                    </el-radio>
                </el-radio-group>

            </div>


            <template v-if="is_wallet == 2">

                <h3>Payment settings</h3>
                <div class="store-payment">
                    <el-radio-group v-model="selectStore.auto_pay" @change="payChange($event)">
                        <el-radio :label="true">
                            <strong>Turn on automatic payments</strong>
                            <span>The costs of your retail orders will be automatically paid with POD wallet when your
                                wallet balance is sufficient.</span>
                        </el-radio>

                        <div class="store-payment-remote" :class="{ active: selectStore.remote_auto_pay }"
                             v-if="selectStore.auto_pay">
                            <em @click="selectStore.remote_auto_pay = !selectStore.remote_auto_pay"></em>
                            <span>Automatic payments for</span>
                            <a @click="remote = true">remote area</a>
                            <span>orders.</span>
                        </div>

                        <el-radio :label="false">
                            <strong>Turn off automatic payments</strong>
                            <span>Pay for your retail orders manually.</span>
                        </el-radio>
                    </el-radio-group>
                </div>


            </template>


            <h3>Shipping settings</h3>
            <div class="store-shipping">
                <el-radio-group v-model="selectStore.transfer_set">
                    <el-radio :label="1">
                        <strong>Standard shipping</strong>
                        <span>Set PODpartner standard shipping as a default shipping method.</span>
                    </el-radio>
                    <el-radio :label="3">
                        <strong>Fast shipping</strong>
                        <span>Set PODpartner fast shipping as a default. Standard shipping will be applied when fast
                            shipping is
                            unavailable.</span>
                    </el-radio>
                    <el-radio :label="2">
                        <strong>Express shipping</strong>
                        <span>Set PODpartner express shipping as a default. Fast shipping will be applied when express
                            shipping is
                            unavailable.</span>
                    </el-radio>
                </el-radio-group>
            </div>

            <!-- woo -->
            <template v-if="selectStore.shop_type == 4">
                <h3>
                    <span>Integration configuration</span>
                    <a @click="woo = selectStore">Edit</a>
                </h3>

                <div class="store-disconnect-woo">
                    <p>
                        <span>Your store's web address, Type in your store URL</span>
                        <strong v-text="selectStore.shop_url"></strong>
                    </p>
                    <p>
                        <span>Consumer key</span>
                        <strong v-text="selectStore.consumer_key"></strong>
                    </p>
                    <p>
                        <span>Consumer secret</span>
                        <strong v-text="selectStore.consumer_secret"></strong>
                    </p>
                </div>
            </template>

            <h3>Disconnect store</h3>
            <div class="store-disconnect-other">
                <StoreIconConnect :type="selectStore.shop_type" />
                <p>
                    <strong v-text="selectStore.shop_name_text"></strong>
                    <span>Store link: {{ selectStore.shop_url }}</span>
                </p>
                <el-popover placement="top" v-model="popover" class>
                    <p>
                        <strong>Disconnect your store?</strong>
                        <span>Once you disconnect your store, all the data associated with it will be lost and cannot be
                            restored.</span><br />
                        <button type="test" @click="popover = false">Cancel</button>
                        <button type="primary" @click="disconnect()">Confirm</button>
                    </p>
                    <button slot="reference">Disconnect</button>
                </el-popover>
            </div>

            <div class="store-submit" :class="{ active: changed }">
                <cite><button type="primary" @click="setSetting()">Save</button></cite>
            </div>

            <UxLoading2 :loading="loading" />

        </template>

        <StoreSettingWoo v-model="woo" />

        <LogisticsRemoteSign v-model="remote" />

    </div>
</template>
<script>
import { mapGetters, mapState } from "vuex";
export default {
    name: "UserSettingsStore",
    layout: "layout-user",
    data () {
        return {

            inited: false,
            loading: false,
            stores: [],
            cache: [],
            select: 0,
            woo: null,
            popover: false,
            remote: false,



            // branding: null,// 增值服务
            // hangTags: null,// 吊牌数据
            // packages: null,// 包装数据


            // auto_pay_class: false,
            // stores_loading: true,
            // stores_error: "",
            // stores: [],

            // loading: true,
            // setting: {
            //     auto_pay: false,
            //     is_ignor: false,
            //     order_import_set: 0,
            //     transfer_set: false,

            // },

            // cache: null,
        };
    },
    computed: {
        ...mapState('user', {
            is_wallet: 'is_wallet',
        }),
        selectStore () {
            let list = this.stores;
            return list.find((i) => i.id == this.select) || list[0];
        },
        // 有数据改变
        changed () {
            const store = this.stores.find((i) => i.id == this.select) || {}
            const cache = this.cache.find((i) => i.id == this.select) || {}

            const map = new Map([...Object.keys(store).map(key => [key, true]), ...Object.keys(cache).map(key => [key, true])])
            const keys = Array.from(map.keys())

            const change = keys.find(i => store[i] != cache[i])
            return !!change

        },



    },
    methods: {
        // handleBrandingChange (data) {
        //     this.branding = data
        // },
        // // 构造增值服务需要的数据
        // getBranding () {
        //     let store = this.selectStore;
        //     let tag_design_id = store.tag_design_id;
        //     let pack_design_id = store.pack_design_id;
        //     let hangTags = this.hangTags || [];
        //     let packages = this.packages || [];
        //     let tag_design_info = hangTags.filter(i => i.id == tag_design_id)[0] || null;
        //     let pack_design_info = packages.filter(i => i.id == pack_design_id)[0] || null;
        //     this.branding = {
        //         shop_id: store.id,
        //         tag_design_id,
        //         pack_design_id,
        //         tag_design_info,
        //         pack_design_info
        //     }
        //     console.log(this.branding);
        // },

        // // 获取吊牌和包装数据
        // async getHangTags () {
        //     let response = await this.$http("BrandHangtag.search", {});
        //     if (response.code === 0) {
        //         this.hangTags = response.data || [];
        //         this.formatTagBag(this.hangTags)
        //     } else {
        //         this.$uxMessage.error(response.msg || "error");
        //     }
        // },
        // async getPackages () {
        //     let response = await this.$http("BrandPackage.search", {});
        //     if (response.code === 0) {
        //         this.packages = response.data || [];
        //         this.formatTagBag(this.packages)
        //     } else {
        //         this.$uxMessage.error(response.msg || "error");
        //     }
        // },

        // formatTagBag (data) {
        //     data.map(item => {
        //         item.shop_list = item.shop_list || []
        //         try {
        //             item.design_list = JSON.parse(item.design_list || '[]');
        //             item.design_list.map(piece => piece.layers = JSON.parse(piece.layers || '[]'))
        //             item.model_img = JSON.parse(item.model_img || '[]');
        //         } catch (error) {
        //             console.log(error)
        //         }
        //     })
        // },

        // 获取商店列表
        async getStore () {
            this.loading = true;
            const select = this.select;
            const response = await this.$http("UserStore.getAllLinkStore");
            if (response.code == 0) {
                this.stores = response.data || [];
                this.cache = this.stores.map(s => ({ ...s }))
                this.select = this.stores.find(s => s.id == select) ? select : (this.stores[0] || {}).id || 0;
                this.loading = false;
            } else {
                this.loading = false;
            }
        },


        // 解除绑定
        async disconnect () {

            this.loading = true;
            this.popover = false;
            let response = await this.$http(
                "UserStore.delLinkStore",
                this.selectStore.id
            );

            if (response.code === 0) {
                let index = this.stores.findIndex(s => s.id == this.select);
                this.stores.splice(index, 1);
                this.$uxMessage.success("Shop deleted successfully");
                this.select = (this.stores[0] || {}).id || 0
                this.popover = false;
                this.loading = false;
            } else {
                this.popover = false;
                this.loading = false;
                this.$uxMessage.error("Failed to delete shop");
            }
        },


        // 更新信息
        async setSetting () {
            this.loading = true;
            let request = {
                auto_pay: this.selectStore.auto_pay,
                remote_auto_pay: this.selectStore.remote_auto_pay,
                is_ignor: this.selectStore.is_ignor,
                // order_import_set: this.selectStore.order_import_set,
                transfer_set: this.selectStore.transfer_set,
                // shop_url: this.selectStore.shop_url,
                // consumer_key: this.selectStore.consumer_key,
                // consumer_secret: this.selectStore.consumer_secret,
            };

            let response = await this.$http(
                "UserStore.setShopSetting/" + this.select,
                request
            );

            if (response.code == 0) {
                this.cache = this.stores.map(s => ({ ...s }))
                this.loading = false;
                this.$uxMessage.success("Template update shop");
            } else {
                this.loading = false;
                this.$uxMessage.error(response.msg || "Failed to update shop");
            }
        },

        payChange (value) {
            if (!value) {
                this.selectStore.remote_auto_pay = false;
            }
        }
    },
    mounted () {

        this.select = (this.$route.query || {}).id || 0;
        this.getStore().then(() => {
            this.inited = true
            this.$nextTick(() => {
                const action = (this.$route.query || {}).action;
                const elem = this.$el.querySelector('.store-payment')

                if (action == 'autopay' && elem) {
                    setTimeout(() => elem.setAttribute('beat', 'play'), 1000);
                    setTimeout(() => elem.setAttribute('beat', 'none'), 2000);
                    setTimeout(() => elem.setAttribute('beat', 'play'), 3000);
                    setTimeout(() => elem.setAttribute('beat', 'none'), 4000);
                }
            })
        })

    }
};
</script>
<style lang="scss" scoped>
.store {
    display: flex;
    flex-direction: column;
    position: relative;

    &>.initing {
        flex: 1;
    }

    &>.loading {
        position: fixed;
        left: 228px;
        top: 0;
        bottom: 0;
        right: 0;
    }

    &>h2 {
        display: flex;
        align-items: center;
        font-size: 36px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 40px;
    }

    &>h3 {
        display: flex;
        margin-top: 40px;
        margin-bottom: 18px;
        font-size: 18px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 18px;

        a {
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #087196;
            line-height: 18px;
            margin-left: 6px;
            text-decoration: underline;
            cursor: pointer;

            &:hover {
                text-decoration: none;
                color: #287e9c;
            }
        }
    }
}

.store-shops {
    margin-top: 0;

    &::v-deep {
        .store-shops-tabs>ul {
            margin-left: -4px;
            margin-right: -4px;
            margin-top: -4px;

            &>li {
                margin: 4px;
                height: 44px;
            }
        }

        .user-search-button {
            display: none;
        }
    }
}

.el-radio-group ::v-deep {
    padding: 28px 28px;
    box-sizing: border-box;

    .el-radio {
        display: flex;

        &+.el-radio {
            margin-top: 24px;
        }
    }

    .el-radio__inner {
        width: 18px;
        height: 18px;
        background: #ffffff;
        border-radius: 9px;
        border: 2px solid #919191;

        &::after {
            width: 6px;
            height: 6px;
        }
    }

    .el-radio.is-checked .el-radio__inner {
        border-color: #ea4c89;
        background-color: transparent;
        box-shadow: none !important;

        &:after {
            background-color: #ea4c89;
        }
    }

    .el-radio__label {
        position: relative;
        display: flex;
        flex-direction: column;

        strong {
            font-size: 14px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #000000;
            line-height: 18px;
            white-space: normal;
        }

        span {
            margin-top: 6px;
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #555555;
            line-height: 22px;
            white-space: normal;
        }

        small {
            position: absolute;
            left: 100%;
            top: 0;
            margin-left: 40px;
            line-height: 18px;

            a {
                color: inherit;
                text-decoration: underline;
            }
        }
    }
}

.store-setting {
    background: #ffffff;
    border-radius: 8px;
    border: 1px solid #e5e5e5;
    overflow: hidden;

    &>p {
        padding: 28px 54px;
        display: flex;
        flex-direction: column;
        position: relative;
        border-bottom: 1px solid #e6e6e6;

        strong {
            font-size: 14px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #000000;
            line-height: 18px;
        }

        span {
            margin-top: 6px;
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #555555;
            line-height: 22px;
        }

        svg {
            position: absolute;
            left: 24px;
            top: 28px;
            font-size: 12px;
            line-height: 17px;
            text-align: center;
            height: 18px;
            width: 18px;
            box-sizing: border-box;
            padding: 4px;
            color: #fff;
            background: #ea4c89;
            border-radius: 50%;
        }

        &+.el-radio-group {
            padding-left: 54px;
            background: #fcfcfc;
        }
    }
}

.store-payment,
.store-shipping {
    background: #ffffff;
    border-radius: 8px;
    border: 1px solid #e5e5e5;
}

.store-payment {
    transition: all 0.6s;

    &[beat='play'] {
        background: #f0f6fa;
    }

    .store-payment-remote {
        display: flex;
        align-items: center;
        font-size: 14px;
        padding: 16px 0;
        margin-left: 30px;

        a {
            padding: 0 4px;
            color: #087196;
            cursor: pointer;
            text-decoration: underline;

            &:hover {
                text-decoration: none;
                color: #287e9c;
            }
        }

        em {
            width: 38px;
            height: 20px;
            margin-right: 6px;
            background: #d6d6d6;
            border-radius: 12px;
            border: 1px solid #d6d6d6;
            box-sizing: border-box;
            position: relative;
            cursor: pointer;
            transition: all 0.2s;

            &:hover {
                background: #dbdbdb;
            }

            &::before {
                content: '';
                position: absolute;
                left: 1px;
                top: 1px;
                width: 16px;
                height: 16px;
                background: #fcfcfc;
                box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.1);
                border-radius: 11px;
                transition: all 0.2s;
            }
        }

        &.active {
            em {
                background: #ea4c89;
                border: 1px solid #d5417a;

                &:hover {
                    background: #c94175;
                }

                &::before {
                    left: 20px;
                }
            }
        }
    }
}

.store-disconnect-woo {
    background: #ffffff;
    border-radius: 8px;
    border: 1px solid #e5e5e5;
    padding: 28px;

    p {
        display: flex;
        flex-direction: column;

        &+p {
            margin-top: 24px;
        }

        span {
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #000000;
            line-height: 18px;
        }

        strong {
            margin-top: 12px;
            height: 40px;
            max-width: 460px;
            background: #f6f6f6;
            border-radius: 8px;
            border: 1px solid #e0e0e0;
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #9e9e9e;
            box-sizing: border-box;
            line-height: 38px;
            padding: 0 10px;
        }
    }
}

.store-disconnect-other {
    background: #ffffff;
    border-radius: 8px;
    border: 1px solid #e5e5e5;
    padding: 26px;
    display: flex;

    .store-icon-connect {
        margin-right: 16px;

        &::v-deep {
            img {
                width: 40px;
                height: 40px;
            }

            strong,
            span {
                display: none;
            }
        }
    }

    p {
        flex: 1;
        display: flex;
        flex-direction: column;

        strong {
            font-size: 14px;
            font-family: Roboto-Medium, Roboto;
            font-weight: 500;
            color: #000000;
            line-height: 18px;
        }

        span {
            margin-top: 6px;
            font-size: 14px;
            font-family: Roboto-Regular, Roboto;
            font-weight: 400;
            color: #757575;
            line-height: 18px;
        }
    }

    button {
        width: 120px;
    }
}

.store-submit {
    position: fixed;
    left: 228px;
    right: 0;
    bottom: 0;
    height: 72px;
    background: #ffffff;
    border-top: 1px solid #e5e5e5;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: all 0.2s;

    button {
        width: 200px;
        height: 40px;
    }

    &.active {
        opacity: 1;
        pointer-events: all;
    }
}

.el-popover {
    background: none;

    p {
        width: 388px;
        height: 164px;
        box-sizing: border-box;
        margin: -12px;
        background: #faf7f0;
        box-shadow: 0px 6px 12px 0px rgba(0, 0, 0, 0.12);
        border-radius: 10px;
        border: 1px solid #efece1;
        text-align: center;
        padding: 20px 18px;
    }

    strong {
        display: block;
        font-size: 14px;
        font-family: Roboto-Medium, Roboto;
        font-weight: 500;
        color: #000000;
        line-height: 20px;
    }

    span {
        display: block;
        margin-top: 12px;
        font-size: 14px;
        font-family: Roboto-Regular, Roboto;
        font-weight: 400;
        color: #555555;
        line-height: 20px;
    }

    button {
        margin: 0 4px;
        width: 96px;
        height: 28px;
        border-radius: 6px;
        white-space: nowrap;

        &[type='test'] {
            background: #fff;
        }
    }
}
</style>

<style>
.el-popover {
    border-radius: 10px;
    background: none;
}

.popper__arrow::after {
    border-top-color: #faf7f0 !important;
}
</style>
